﻿using Data.Db;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System;
using Data.Api.Db;
using Data.Api.Repositories;
using Data.Mappers;

namespace Data.Repositories
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T1">Entity</typeparam>
    /// <typeparam name="T2">DatabaseModel</typeparam>
    public abstract class Repository<T1, T2> : IRepository<T1> where T2 : class
    {
        protected readonly IDbShopConcreteContext context;
        protected readonly IDbSet<T2> dbSet;
        protected readonly IMapper<T1, T2> mapper;

        public Repository(IDbShopConcreteContext context, IDbSet<T2> dbSet, IMapper<T1, T2> mapper)
        {
            this.context = context;
            this.dbSet = dbSet;
            this.mapper = mapper;
        }


        public IQueryable<T1> All
        {
            get 
            { 
                return mapper.WrapperData(dbSet.ToList()).AsQueryable(); 
            }
        }

        public T1 Find(int id)
        {
            return mapper.WrapperData(dbSet.Find(id));
        }

        public abstract void Insert(T1 entity);

        public abstract void Update(T1 entity);

        public void Delete(int id)
        {
            T2 entity = dbSet.Find(id);
            dbSet.Remove(entity);
        }
    }
}
